//
//  GTNotification.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/29.
//

#import <UserNotifications/UserNotifications.h>
#import "GTNotification.h"
#import <UIKit/UIKit.h>
@interface GTNotification ()<UNUserNotificationCenterDelegate>
-(void)_pushLocalNotification;
@end

@implementation GTNotification
+ (GTNotification *)notificationManager {
    static GTNotification *manager;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        manager = [[GTNotification alloc]init];
    });
    return manager;
}

- (instancetype)init {
    self = [super init];

    if (self) {
       
    }

    return self;
}

- (void)checkNotificationAuthorization {
	UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
	center.delegate = self;
	[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge
																		completionHandler:^(BOOL granted, NSError *_Nullable error) {
		NSLog(@"");
		if(granted){
//			[self _pushLocalNotification];
			dispatch_async(dispatch_get_main_queue(), ^{
				[[UIApplication sharedApplication] registerForRemoteNotifications];
			});
		}
	}];
}



-(void)_pushLocalNotification{
	UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
	content.badge = @(1);
	content.title= @"极客时间";
	content.body = @"this is my IOS App";
	UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:30.f repeats:NO];
	UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"id" content:content trigger:trigger];
	[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
			NSLog(@"");
	}];
}

#pragma mark - UNUserNotificationCenterDelegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
	completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
	completionHandler();
}
@end
